<template>
    <a-modal :title="title" :width="900" :height="360" :visible="visible" :confirmLoading="loading"
        @ok="handleOK" @cancel="() => { $emit('cancel') }">
        <a-spin :spinning="loading">
            <a-form :form="form" v-bind="formLayout">
                <a-form-item label="id" style="display: none;">
                    <a-input v-decorator="['id']" />
                </a-form-item>
                <a-form-item label="dictId" style="display: none;">
                    <a-input v-decorator="['dictId']" />
                </a-form-item>
                <a-form-item label="字典名称">
                    <a-input :disabled="true" v-decorator="['name', { rules: [{ required: true, message: '请输入分类名称' }] }]" />
                </a-form-item>
                <a-form-item label="标签">
                    <a-input v-decorator="['label', { rules: [{ required: true, message: '请输入标签' }] }]" />
                </a-form-item>
                <a-form-item label="标签值">
                    <a-input v-decorator="['value', { rules: [{ required: true, message: '请输入标签值' }] }]" />
                </a-form-item>
                <a-form-item label="排序权重">
                    <a-input v-decorator="['sort', { rules: [{ required: true, message: '请输入排序权重' }] }]" />
                </a-form-item>
            </a-form>
        </a-spin>
    </a-modal>
</template>
  
<script>
import pick from 'lodash.pick'
// 表单字段
const fields = ['name', 'id', 'label', 'dictId', 'sort', 'value']
export default {
    props: {
        visible: {
            type: Boolean,
            required: true
        },
        loading: {
            type: Boolean,
            default: () => false
        },
        model: {
            type: Object,
            default: () => null
        }
    },
    data() {
        this.formLayout = {
            labelCol: {
                xs: { span: 24 },
                sm: { span: 3 }
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 18 }
            }
        }
        return {
            title: '新增',
            form: this.$form.createForm(this)
        }
    },
    created() {
        // 防止表单未注册
        fields.forEach(v => this.form.getFieldDecorator(v))

        // 当 model 发生改变时，为表单设置值
        this.$watch('model', () => {
            if (this.model) {
                this.form.setFieldsValue(pick(this.model, fields))
            }
        })
    },
    methods: {
        handleOK() {
            this.$emit('ok')
        }
    }
}
</script>
<style scoped>
</style>
  